'use strict';

const Controller = require('egg').Controller;

class ResumeFavoriteController extends Controller {
  /**
   * 收藏简历
   * POST /api/favorites
   */
  async create() {
    const { ctx } = this;
    
    try {
      const body = ctx.request.body;
      // 从JWT token中获取用户ID
      const userId = ctx.user?.id;
      if (!userId) {
        ctx.body = { code: 1, msg: '未登录', data: null };
        return;
      }

      // 验证必填字段
      if (!body.resume_type) {
        ctx.body = { code: 1, msg: '简历类型不能为空', data: null };
        return;
      }

      if (body.resume_type === 1 && !body.resume_id) {
        ctx.body = { code: 1, msg: '简历ID不能为空', data: null };
        return;
      }

      if (body.resume_type === 2 && !body.pdf_resume_id) {
        ctx.body = { code: 1, msg: 'PDF简历ID不能为空', data: null };
        return;
      }

      const data = {
        user_id: userId,
        resume_id: body.resume_id || null,
        pdf_resume_id: body.pdf_resume_id || null,
        resume_name: body.resume_name || '',
        template_id: body.template_id || null,
        pdf_url: body.pdf_url || null,
        resume_type: body.resume_type
      };

      const result = await ctx.service.resumeFavorite.addFavorite(data);

      if (result.success) {
        ctx.body = { code: 0, msg: '收藏成功', data: result.data };
      } else {
        ctx.body = { code: 1, msg: result.msg, data: null };
      }
    } catch (error) {
      ctx.logger.error('收藏简历失败:', error);
      ctx.body = { code: 1, msg: '收藏失败：' + error.message, data: null };
    }
  }

  /**
   * 获取收藏列表
   * GET /api/favorites
   */
  async index() {
    const { ctx } = this;

    try {
      // 从JWT token中获取用户ID
      const userId = ctx.user?.id;
      if (!userId) {
        ctx.body = { code: 1, msg: '未登录', data: null };
        return;
      }
      const page = parseInt(ctx.query.page || 1);
      const pageSize = parseInt(ctx.query.pageSize || 10);

      const result = await ctx.service.resumeFavorite.getFavoriteList(userId, page, pageSize);

      if (result.success) {
        ctx.body = { code: 0, msg: '获取成功', data: result.data };
      } else {
        ctx.body = { code: 1, msg: result.msg, data: null };
      }
    } catch (error) {
      ctx.logger.error('获取收藏列表失败:', error);
      ctx.body = { code: 1, msg: '获取收藏列表失败：' + error.message, data: null };
    }
  }

  /**
   * 取消收藏
   * DELETE /api/favorites/:id
   */
  async destroy() {
    const { ctx } = this;

    try {
      const favoriteId = parseInt(ctx.params.id);
      // 从JWT token中获取用户ID
      const userId = ctx.user?.id;
      if (!userId) {
        ctx.body = { code: 1, msg: '未登录', data: null };
        return;
      }

      const result = await ctx.service.resumeFavorite.removeFavorite(favoriteId, userId);

      if (result.success) {
        ctx.body = { code: 0, msg: '取消收藏成功', data: null };
      } else {
        ctx.body = { code: 1, msg: result.msg, data: null };
      }
    } catch (error) {
      ctx.logger.error('取消收藏失败:', error);
      ctx.body = { code: 1, msg: '取消收藏失败：' + error.message, data: null };
    }
  }

  /**
   * 检查是否已收藏
   * GET /api/favorites/check
   */
  async check() {
    const { ctx } = this;

    try {
      // 从JWT token中获取用户ID
      const userId = ctx.user?.id;
      if (!userId) {
        ctx.body = { code: 1, msg: '未登录', data: null };
        return;
      }
      const resumeId = ctx.query.resume_id ? parseInt(ctx.query.resume_id) : null;
      const pdfResumeId = ctx.query.pdf_resume_id ? parseInt(ctx.query.pdf_resume_id) : null;

      if (!resumeId && !pdfResumeId) {
        ctx.body = { code: 1, msg: '简历ID或PDF简历ID不能同时为空', data: null };
        return;
      }

      const result = await ctx.service.resumeFavorite.checkFavorite(userId, resumeId, pdfResumeId);

      if (result.success) {
        ctx.body = { code: 0, msg: '检查成功', data: { isFavorite: result.isFavorite, favoriteId: result.favoriteId } };
      } else {
        ctx.body = { code: 1, msg: result.msg, data: null };
      }
    } catch (error) {
      ctx.logger.error('检查收藏状态失败:', error);
      ctx.body = { code: 1, msg: '检查收藏状态失败：' + error.message, data: null };
    }
  }
}

module.exports = ResumeFavoriteController;

